package com.thrc.borrow.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.thrc.borrow.entity.City;
import com.thrc.borrow.service.CityService;
import com.thrc.borrow.utils.CacheUtil;
import com.thrc.borrow.utils.Container;
import com.thrc.borrow.utils.ResponseUtil;
import com.thrc.borrow.utils.ReturnCode;
import com.thrc.borrow.vo.CityVo;

@Controller
@RequestMapping("/city")
public class CityController {

	@Autowired
	private CityService cityService;

	@RequestMapping("/goCityJsp")
	public String goCityJsp() {
		return "sys/cityList";
	}

	@RequestMapping(value = "/getTypeJson")
	public void getTypeJson(HttpServletResponse response) {
		JSONArray retJson = new JSONArray();

		JSONObject COUNTRY = new JSONObject();
		COUNTRY.put(ReturnCode.RETURN_CODE, Container.CITY_STATUS.COUNTRY);
		COUNTRY.put(ReturnCode.RETURN_DATA, "国别");

		JSONObject PROVINCE = new JSONObject();
		PROVINCE.put(ReturnCode.RETURN_CODE, Container.CITY_STATUS.PROVINCE);
		PROVINCE.put(ReturnCode.RETURN_DATA, "省份");

		JSONObject CITY = new JSONObject();
		CITY.put(ReturnCode.RETURN_CODE, Container.CITY_STATUS.CITY);
		CITY.put(ReturnCode.RETURN_DATA, "市级");

		JSONObject DISTRICT = new JSONObject();
		DISTRICT.put(ReturnCode.RETURN_CODE, Container.CITY_STATUS.DISTRICT);
		DISTRICT.put(ReturnCode.RETURN_DATA, "区级");

		retJson.add(COUNTRY);
		retJson.add(PROVINCE);
		retJson.add(CITY);
		retJson.add(DISTRICT);

		ResponseUtil.print(response, retJson);

	}

	@RequestMapping("/list")
	public void selectByPage(HttpServletResponse response, City t) {
		JSONObject retJson = new JSONObject();
		try {
			int totleCount = cityService.selectCount(t);
			retJson.put("total", totleCount);
			if (totleCount == 0) {
				retJson.put("data", null);
				ResponseUtil.print(response, retJson);
				return;
			}
			List<CityVo> cityList = cityService.selectByPage(t);
			retJson.put("data", cityList);
			ResponseUtil.print(response, retJson);
		} catch (Exception e) {
			e.printStackTrace();
			ResponseUtil.print(response, ReturnCode.FAIL_CODE, e.getMessage());
		}
	}

	@RequestMapping("/listAll")
	public void selectAll(HttpServletResponse response, City t) {
		try {
			List<CityVo> cityList = cityService.selectAll(t);
			cityList.add(0, CityVo.getSelectVo());
			ResponseUtil.print(response, cityList);
		} catch (Exception e) {
			e.printStackTrace();
			ResponseUtil.print(response, ReturnCode.FAIL_CODE, e.getMessage());
		}
	}

	@RequestMapping("/insert")
	public void insert(HttpServletResponse response, City t) {
		try {
			cityService.insert(t);
			ResponseUtil.print(response, ReturnCode.SUCCESS_CODE);
		} catch (Exception e) {
			e.printStackTrace();
			ResponseUtil.print(response, ReturnCode.FAIL_CODE, e.getMessage());
		}
	}

	@RequestMapping("/update")
	public void update(HttpServletResponse response, City t) {
		try {
			cityService.update(t);
			ResponseUtil.print(response, ReturnCode.SUCCESS_CODE);
		} catch (Exception e) {
			e.printStackTrace();
			ResponseUtil.print(response, ReturnCode.FAIL_CODE, e.getMessage());
		}
	}

	@RequestMapping("/delete")
	public void delete(HttpServletResponse response, City t) {
		try {
			cityService.delete(t);
			ResponseUtil.print(response, ReturnCode.SUCCESS_CODE);
		} catch (Exception e) {
			e.printStackTrace();
			ResponseUtil.print(response, ReturnCode.FAIL_CODE, e.getMessage());
		}
	}

	@RequestMapping("/get")
	public void selectById(HttpServletResponse response, City t) {
		ResponseUtil.print(response, cityService.selectById(t));
	}

	@RequestMapping("/getName")
	public void getName(HttpServletResponse response, City t) {

		if (t.getType() == 1) {
			@SuppressWarnings("unchecked")
			Map<Integer, CityVo> retMap = CacheUtil.getCacheDate(Container.CACHE_KEY.SYS_PROVINCE, HashMap.class);
			if (retMap.containsKey(t.getId())) {
				ResponseUtil.print(response, ReturnCode.SUCCESS_CODE, retMap.get(t.getId()).getName());
				return;
			}
		}

		if (t.getType() == 2) {
			@SuppressWarnings("unchecked")
			Map<Integer, CityVo> retMap = CacheUtil.getCacheDate(Container.CACHE_KEY.SYS_CITY, HashMap.class);
			if (retMap.containsKey(t.getId())) {
				ResponseUtil.print(response, ReturnCode.SUCCESS_CODE, retMap.get(t.getId()).getName());
				return;
			}
		}

		if (t.getType() == 3) {
			@SuppressWarnings("unchecked")
			Map<Integer, CityVo> retMap = CacheUtil.getCacheDate(Container.CACHE_KEY.SYS_DISTRICT, HashMap.class);
			if (retMap.containsKey(t.getId())) {
				ResponseUtil.print(response, ReturnCode.SUCCESS_CODE, retMap.get(t.getId()).getName());
				return;
			}
		}

	}
}
